package com.chen.springsecurity.expression;


import com.alibaba.fastjson.JSON;
import com.chen.springsecurity.domain.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Y·C
 * @version 1.0.0
 * @ClassName SecurityExpressionRoot.java
 * @Description 自定义权限校验表达式
 * @createTime 2023年08月30日 11:35:00
 */
@Component("ex")
public class YcExpressionRoot {

    public boolean hasAuthority(String authority) {
        //获取当前用户的权限
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        //获取用户信息
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        //获取用户权限集合
        List<String> permissions = loginUser.getPermissions();


        //判断用户权限集合中是否存在authority
        return permissions.contains(authority);

    }



}
